import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
// 路由表
const routes = [
  // 重定向
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/index",
    component: () => import("../views/Index.vue"),
    // 二级路由
    children: [
      {
        path: "/index",
        redirect: "/index/home",
      },
      {
        path: "/index/home",
        component: () => import("../views/index/Home.vue"),
        children: [
          {
            path: "/index/home",
            redirect: "/index/home/hot",
          },
          {
            path: "/index/home/hot",
            component: () => import("../components/home/MyMovies.vue"),
            meta: {
              title: "猫眼电影-娱乐看猫眼",
            },
          },
          {
            path: "/index/home/cinema",
            component: () => import("../components/home/cinema.vue"),
            meta: {
              title: "首页_影院",
            },
          },
          {
            path: "/index/home/classics",
            component: () => import("../components/home/classics.vue"),
            meta: {
              title: "首页_经典电影",
            },
          },
          {
            path: "/index/home/wait",
            component: () => import("../components/home/wait.vue"),
            meta: {
              title: "首页_待映",
            },
          },
        ],
      },
      {
        path: "/index/videos",
        component: () => import("../views/index/Videos.vue"),
        meta: {
          title: "猫眼视频",
        },
      },
      {
        path: "/index/minvideos",
        component: () => import("../views/index/MinVideos.vue"),
        meta: {
          title: "猫眼小视频",
        },
      },
      {
        path: "/index/show",
        component: () => import("../views/index/Show.vue"),
        meta: {
          title: "猫眼演出",
        },
      },
      {
        path: "/index/myself",
        component: () => import("../views/index/Myself.vue"),
        meta: {
          mustlogin: true,
          title: "我的",
        },
      },
    ],
  },
  {
    path: "/index/home/city",
    component: () => import("../views/City.vue"),
    meta: {
      title: "城市选择",
    },
  },
  {
    path: "/detail/:id",
    component: () => import("../views/Detail.vue"),
  },
  {
    path: "/buyticket/:id",
    component: () => import("../views/Buyticket.vue"),
  },
  {
    path: "/login",
    component: () => import("../views/index/Login.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "*",
    component: () => import("../views/NotFound404.vue"),
    meta: {
      title: "网页丢失",
    },
  },
];
// Vue路由实例化
const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  document.title = to.meta.title || "猫眼电影——娱乐看猫眼";
  if (to.meta.mustlogin) {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
export default router;
